<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function test()
    {
        /*  $array = array(
              array('id' => 1, 'pid' => 0),
              array('id' => 2, 'pid' => 0),
              array('id' => 3, 'pid' => 1),
              array('id' => 4, 'pid' => 2),
              array('id' => 5, 'pid' => 2),
              array('id' => 6, 'pid' => 4),
              array('id' => 7, 'pid' => 4),
              array('id' => 8, 'pid' => 3),
              array('id' => 9, 'pid' => 1),
              array('id' => 10, 'pid' => 8),
              array('id' => 11, 'pid' => 0)
          );*/


        die;
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        //获取分类数据
        $model = model("category");
        $result = array("status" => 200, "msg" => "ok", "data" => []);

        $list = $model->where("status='normal'")->where("pid=0")->field("id,name,image,type,flag,menu")->select();
        //拼接前端跳转地址 1 维修教程 维修案例 2 我的收藏 3 备件检索
        foreach ($list as &$vv) {
            if ($vv["menu"] == 1) {
                $vv["pageUrl"] = "/pages/lists/listmenu?cid={$vv['id']}&cname={$vv['name']}";
            } elseif ($vv["menu"] == 2) {//我的收藏
                $vv["pageUrl"] = "/pages/mycoperion/mycoperion";
            } else if ($vv["menu"] == 3) {
                $vv["pageUrl"] = "/pages/coperion/coperion";
            } else {
                $vv["pageUrl"] = "";
            }
        }


        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }


    }

    //获取分类下二级分类
    public function getClass()
    {
        $params = $this->request->get();
        $fillter = json_decode(htmlspecialchars_decode($params["fillter"]), true);
        $ctype = $fillter["ctype"];

        $where["pid"] = $fillter["cid"];

        $list = db("category")->field("id,name")->where($where)->select();

        foreach ($list as &$v) {
            $v["ctype"] = $ctype;
        }

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //获取文章内容
    public function getSearchDetail()
    {
        $params = $this->request->post()["fillter"];

        $model = model("coperion");
        $info = $model->where("status='normal'")->where("id={$params['cid']}")->find();
        //判断用户是否已经收藏
        $where['aid'] = $params['cid'];
        $where['uid'] = $params['uid'];
        $where['type'] = 1;//收藏
        $where['contype'] = 2;//检索
        $has = model("collect")->where($where)->find();
        if ($has) {
            $info["has_collect"] = 1;//已收藏
        } else {
            $info["has_collect"] = 2;
        }

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($info) {
            $result["data"] = $info;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //获取文章内容
    public function getArticleDetail()
    {
        $params = $this->request->post()["fillter"];

        $model = model("attachment");
        $info = $model->where("status='normal'")->where("id={$params['aid']}")->field("id,title,url,status,catename,imagetype,content")->find();
        //判断用户是否已经收藏
        $where['aid'] = $params['aid'];
        $where['uid'] = $params['uid'];
        $where['type'] = 1;//收藏
        $has = model("collect")->where($where)->find();
        if ($has) {
            $info["has_collect"] = 1;//已收藏
        } else {
            $info["has_collect"] = 2;
        }

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($info) {
            $result["data"] = $info;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //获取数据列表
    public function getProductList()
    {
        $params = $this->request->get();
        $fillter = json_decode(htmlspecialchars_decode($params["fillter"]), true);

        $cid = $fillter["cid"];
        $catelist = db("category")->field('id,pid')->where(['status' => 'normal'])->select();
        $pids = getUpTree($catelist, $cid);

        $where = [];


        if ($fillter && $fillter["title"] != '') {
            $where["title"] = array('like', "%{$fillter['title']}%");
        }


        $model = model("attachment");

        $list = $model->where("status='normal'")->whereIn('cate_id', $pids, 'AND')->where($where)->field("id,title,url,status,catename,imagetype")->page($fillter["page"], $fillter["limit"])->select();


        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //获取数据列表
    public function getProductListold()
    {
        $params = $this->request->get();
        $fillter = json_decode(htmlspecialchars_decode($params["fillter"]), true);

        $where = [];
        if ($fillter && $fillter["catename"] != '') {
            $where["catename"] = $fillter["catename"];
        }

        if ($fillter && $fillter["title"] != '') {
            $where["title"] = array('like', "%{$fillter['title']}%");
        }


        $model = model("attachment");

        $list = $model->where("status='normal'")->where($where)->field("id,title,url,status,catename,imagetype")->page($fillter["page"], $fillter["limit"])->select();


        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //临时存储首次检索数据 根据uid
    public function addSearch($uid, $idata)
    {

        $db = db("coperion_tmp");
        $db->where(["uid" => $uid])->delete();
        $db->insertGetId($idata);
    }

    //二级检索
    public function getSearchListthree()
    {
        $params = $this->request->post();
        $fillter = $params["fillter"];
        $db = db("coperion");

        $where = [];
        $wheretwo = [];
        $wherethr = [];

        if (key_exists("manufacturer", $fillter) && $fillter["manufacturer"] != '') {
            $where["manufacturer"] = array('like', "%{$fillter['manufacturer']}%");
        }
        if (key_exists("manufacturercode", $fillter) && $fillter["manufacturercode"] != '') {
            $where["manufacturercode"] = array('like', "%{$fillter['manufacturercode']}%");
        }
        if (key_exists("devmanufacturer", $fillter) && $fillter["devmanufacturer"] != '') {
            $where["devmanufacturer"] = array('like', "%{$fillter['devmanufacturer']}%");
        }
        if (key_exists("devmanufacturercode", $fillter) && $fillter["devmanufacturercode"] != '') {
            $where["devmanufacturercode"] = array('like', "%{$fillter['devmanufacturercode']}%");
        }

        if (key_exists("nowstock", $fillter) && $fillter["nowstock"] != '') {
            $where["nowstock"] = array('egt', "{$fillter['nowstock']}");

        }

        //基础关键词
        if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
            $where["desc"] = array('like', "%{$fillter['desc']}%");

        }

        //二次检索
        if ($fillter["contain"] == 1) {//包含
            if($fillter['contain_code'] == 1){ //包含原型号
                $wherethr["dccode"] = array('like', "%{$fillter['desc']}%");
                $wherefour["desc"] = array('like', "%{$fillter['desc']}%");
                if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                    $wherethr["desc"] = array('like', "%{$fillter['desctwo']}%");
                }

                $list = $db
                    ->where($where)
                    ->where($wheretwo)
                    ->where($wherefour)
                    ->whereor($wherethr)
                    ->page($fillter["page"], $fillter["limit"])
                    ->select();

            }else{
                $wheretwo["desc"] = array('like', "%{$fillter['desctwo']}%");

                $list = $db
                    ->where($where)
                    ->where($wheretwo)
                    ->where($wherethr)
                    ->page($fillter["page"], $fillter["limit"])
                    ->select();
            }

        } elseif ($fillter["contain"] == 2) {//不包含

            if($fillter['contain_code'] == 1){
                $wherethr["dccode"] = array('like', "%{$fillter['desc']}%");
                $wherefour["desc"] = array('like', "%{$fillter['desc']}%");
                if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                    $wherethr["desc"] = array('not like', "%{$fillter['desctwo']}%");
                }

                $list = $db
                    ->where($where)
                    ->where($wheretwo)
                    ->where($wherefour)
                    ->whereor($wherethr)
                    ->page($fillter["page"], $fillter["limit"])
                    ->select();


            }else{
                $wheretwo["desc"] = array('not like', "%{$fillter['desctwo']}%");
                $list = $db
                    ->where($where)
                    ->where($wheretwo)
                    ->where($wherethr)
                    ->page($fillter["page"], $fillter["limit"])
                    ->select();
            }



        }



        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }


    }

    //获取数据列表 检索
    public function getSearchList()
    {
        ini_set ('memory_limit', '1024M');
        $params = $this->request->post();
        $fillter = $params["fillter"];


        $model = model("coperion");
        //查询   由于替换特殊字符后 模糊匹配就行了
        $where = [];
        $where1 = [];
        if ($fillter["contain"] == 9) {//首次 + 首次滚动



                if (key_exists("contain_code", $fillter) && $fillter["contain_code"] == 1) {
                    $where1["dccode"] = array('like', "%{$fillter['desc']}%");

                    if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
                        $where["desc"] = array('like', "%{$fillter['desc']}%");
                    }

                    $list = $model
                        ->where($where)
                        ->whereOr($where1)
                        ->page($fillter["page"], $fillter["limit"])
                        ->select();

                }else{

                    if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
                        $where["desc"] = array('like', "%{$fillter['desc']}%");
                    }

                    $list = $model
                        ->where($where)
                        ->page($fillter["page"], $fillter["limit"])
                        ->select();
                }




                $result = array("status" => 200, "msg" => "ok", "data" => []);
                if ($list) {
                    $list = collection($list)->toArray();
                    $result["data"] = $list;
                    return json($result);

                }else{
                    $result["status"] = 2020;
                    $result["msg"] = "暂无数据";
                    return json($result);
                }


        }


    }

    //获取数据列表 检索
    public function getSearchListold()
    {
        ini_set ('memory_limit', '128M');
        $params = $this->request->post();
        $fillter = $params["fillter"];
        $db = db("coperion_tmp");

        $model = model("coperion");
        //查询   由于替换特殊字符后 模糊匹配就行了

        $where = [];
        $where2 = [];
        $wherere1 = [];
        $wherere2 = [];

        if ($fillter["contain"] == 9) {//首次 + 首次滚动

            if ($fillter["page"] == 1) {


                $db->where(["uid" => $fillter["uid"]])->delete();

                if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
                    $keyword = $fillter["desc"];
                    if (strstr($keyword, "*")) {
                        $re1 = str_replace("*", '', $keyword);
                        $wherere1["desc"] = array('like', "%{$re1}%");
                        $wherere2["dccode"] = array('like', "%{$re1}%");
                    } else {
                        $where["desc"] = array('like', "%{$fillter['desc']}%");
                        $wherere2["dccode"] = array('like', "%{$fillter['desc']}%");
                    }

                }

                if (key_exists("contain_code", $fillter) && $fillter["contain_code"] == 1) {
                    $where["dccode"] = array('neq', "");
                }


                $list = $model
                    ->where($where)
                    ->where($wherere1)
                    ->whereOr($wherere2)
                    ->select();


                if ($list) {
                    $list = collection($list)->toArray();
                    foreach ($list as &$lv) {
                        $lv["uid"] = $fillter["uid"];
                        $lv["keyword"] = $fillter["desc"];
                        $lv["createtime"] = time();
                    }
                    $db->insertAll($list);

                }


            }

            //返回数据
            $reslist = $db
                ->where(["uid" => $fillter["uid"]])
                ->page($fillter["page"], $fillter["limit"])
                ->select();

            $reslist = collection($reslist)->toArray();

            $result = array("status" => 200, "msg" => "ok", "data" => []);

            if ($reslist) {
                $result["data"] = $reslist;
                return json($result);
            } else {
                $result["status"] = 2020;
                $result["msg"] = "暂无数据";
                return json($result);
            }

        }


    }


    //获取数据列表 检索
    public function getSearchList2()
    {
        $params = $this->request->post();

        $fillter = $params["fillter"];

        $where = [];
        $where2 = [];
        $wheretwo = [];
        $wherere1 = [];
        $wherere2 = [];
        $model = model("coperion");

        //由于替换特殊字符后 模糊匹配就行了

        if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
            $keyword = $fillter["desc"];
            if (strstr($keyword, "*")) {
                $re1 = str_replace("*", '', $keyword);
                $wherere1["desc"] = array('like', "%{$re1}%");
                $wherere2["dccode"] = array('like', "%{$re1}%");
            } else {
                $where2["desc"] = array('like', "%{$fillter['desc']}%");
                $wherere2["dccode"] = array('like', "%{$fillter['desc']}%");
            }


        }

        if (key_exists("contain_code", $fillter) && $fillter["contain_code"] == 1) {
            $where["dccode"] = array('neq', "");
        }


        if (key_exists("manufacturer", $fillter) && $fillter["manufacturer"] != '') {
            $where["manufacturer"] = array('like', "%{$fillter['manufacturer']}%");
        }
        if (key_exists("manufacturercode", $fillter) && $fillter["manufacturercode"] != '') {
            $where["manufacturercode"] = array('like', "%{$fillter['manufacturercode']}%");
        }
        if (key_exists("devmanufacturer", $fillter) && $fillter["devmanufacturer"] != '') {
            $where["devmanufacturer"] = array('like', "%{$fillter['devmanufacturer']}%");
        }
        if (key_exists("devmanufacturercode", $fillter) && $fillter["devmanufacturercode"] != '') {
            $where["devmanufacturercode"] = array('like', "%{$fillter['devmanufacturercode']}%");
        }

        if (key_exists("nowstock", $fillter) && $fillter["nowstock"] != '') {
            $where["nowstock"] = array('egt', "{$fillter['nowstock']}");

        }

        //二次检索
        if ($fillter["contain"] == 1) {//包含
            if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                $wheretwo["desc"] = array('like', "%{$fillter['desctwo']}%");
            }

            $list = $model
                ->where($where)
                ->where($wheretwo)
                ->where($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();


            print_r($model->getLastSql());
            die;


        } elseif ($fillter["contain"] == 2) {//不包含
            if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                $wheretwo["desc"] = array('not like', "%{$fillter['desctwo']}%");
            }


            $list = $model
                ->where($where)
                ->where($wheretwo)
                ->where($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();


        } else {

            $list = $model
                ->where($where)
                ->where($wherere1)
                ->whereOr($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();

        }


        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }


    //获取数据列表 检索
    public function getSearchListold2()
    {
        $params = $this->request->post();

        $fillter = $params["fillter"];

        $where = [];
        $where2 = [];
        $wheretwo = [];
        $wherere1 = [];
        $wherere2 = [];
        $model = model("coperion");

        //由于替换特殊字符后 模糊匹配就行了

        if (key_exists("desc", $fillter) && $fillter["desc"] != '') {
            $keyword = $fillter["desc"];
            if (strstr($keyword, "*")) {
                $re1 = str_replace("*", '', $keyword);
                $wherere1["desc"] = array('like', "%{$re1}%");
                $wherere2["dccode"] = array('like', "%{$re1}%");
            } else {
                $where2["desc"] = array('like', "%{$fillter['desc']}%");
                $wherere2["dccode"] = array('like', "%{$fillter['desc']}%");
            }


        }

        if (key_exists("contain_code", $fillter) && $fillter["contain_code"] == 1) {
            $where["dccode"] = array('neq', "");
        }


        if (key_exists("manufacturer", $fillter) && $fillter["manufacturer"] != '') {
            $where["manufacturer"] = array('like', "%{$fillter['manufacturer']}%");
        }
        if (key_exists("manufacturercode", $fillter) && $fillter["manufacturercode"] != '') {
            $where["manufacturercode"] = array('like', "%{$fillter['manufacturercode']}%");
        }
        if (key_exists("devmanufacturer", $fillter) && $fillter["devmanufacturer"] != '') {
            $where["devmanufacturer"] = array('like', "%{$fillter['devmanufacturer']}%");
        }
        if (key_exists("devmanufacturercode", $fillter) && $fillter["devmanufacturercode"] != '') {
            $where["devmanufacturercode"] = array('like', "%{$fillter['devmanufacturercode']}%");
        }

        if (key_exists("nowstock", $fillter) && $fillter["nowstock"] != '') {
            $where["nowstock"] = array('egt', "{$fillter['nowstock']}");

        }

        //二次检索
        if ($fillter["contain"] == 1) {//包含
            if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                $wheretwo["desc"] = array('like', "%{$fillter['desctwo']}%");
            }

            $list = $model
                ->where($where)
                ->where($wheretwo)
                ->where($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();


            print_r($model->getLastSql());
            die;


        } elseif ($fillter["contain"] == 2) {//不包含
            if (key_exists("desctwo", $fillter) && $fillter["desctwo"] != '') {
                $wheretwo["desc"] = array('not like', "%{$fillter['desctwo']}%");
            }


            $list = $model
                ->where($where)
                ->where($wheretwo)
                ->where($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();


        } else {

            $list = $model
                ->where($where)
                ->where($wherere1)
                ->whereOr($wherere2)
                ->page($fillter["page"], $fillter["limit"])
                ->select();

        }


        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }


    //查询 附件表 attachhment
    private function getAttach($id)
    {
        $model = model("attachment");
        return $model->where("id=" . $id)->field('catename,title')->find();
    }

    //查询 检索表 coperion
    private function getCoperion($id)
    {
        $model = model("coperion");
        return $model->where("id=" . $id)->field("id,dccode")->find();
    }


    //获取数据列表 收藏页面
    public function getDatalist()
    {

        $params = $this->request->get();
        $fillter = json_decode(htmlspecialchars_decode($params["fillter"]), true);

        //type 1 收藏 2历史记录
        if ($fillter && $fillter["type"] != '') {
            $where["type"] = $fillter["type"];
        }

        $where["uid"] = $fillter["uid"];

        $model = model("collect");


        $list = $model->where($where)->page($fillter["page"], $fillter["limit"])->order("updatetime desc,createtime desc")->select();
        $list = collection($list)->toArray();

        if ($list) {
            foreach ($list as &$v) {
                if ($v["contype"] == 1) {
                    $info = $this->getAttach($v["aid"]);
                    $v["catename"] = $info["catename"];
                    $v["title"] = $info["title"];
                } else {
                    $info = $this->getCoperion($v["aid"]);
                    $v["catename"] = $info["id"];//不需要
                    $v["title"] = $info["dccode"];
                }

            }
        }


        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //获取数据列表 收藏页面
    public function getDatalistold()
    {

        $params = $this->request->get();
        $fillter = json_decode(htmlspecialchars_decode($params["fillter"]), true);

        //type 1 收藏 2历史记录
        if ($fillter && $fillter["type"] != '') {
            $where["type"] = $fillter["type"];
        }

        $where["uid"] = $fillter["uid"];

        $model = model("collect");

        $list = $model
            ->join("attachment a", 'fa_collect.aid = a.id')
            ->where("a.status='normal'")
            ->where($where)
            ->field("fa_collect.*,a.title,a.url,a.status,a.catename,a.imagetype")
            ->order("fa_collect.updatetime desc,fa_collect.createtime desc")
            ->page($fillter["page"], $fillter["limit"])
            ->select();

        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }


    //添加收藏 维修教程 故障案例
    public function addCollect()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);
        //判断是否已经收藏
        $where["uid"] = $params["uid"];
        $where["aid"] = $params["aid"];
        $where["type"] = 1;
        $model = model("collect");
        $has = $model->where($where)->find();
        if ($has) {
            if ($model->where($where)->delete()) {
                $result["data"] = 1;
                return json($result);
            }
        } else {
            $params["createtime"] = time();
            if ($model->insertGetId($params)) {
                $result["data"] = 2;
                return json($result);
            }
        }

        $result["status"] = 2020;
        $result["msg"] = "操作失败";
        return json($result);


    }

    //添加收藏 备件检索
    public function addCollectsearch()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);
        //判断是否已经收藏
        $where["uid"] = $params["uid"];
        $where["aid"] = $params["aid"];
        $where["type"] = 1;
        $where["contype"] = 2;//检索
        $model = model("collect");
        $has = $model->where($where)->find();
        if ($has) {
            if ($model->where($where)->delete()) {
                $result["data"] = 1;
                return json($result);
            }
        } else {
            $params["createtime"] = time();
            if ($model->insertGetId($params)) {
                $result["data"] = 2;
                return json($result);
            }
        }

        $result["status"] = 2020;
        $result["msg"] = "操作失败";
        return json($result);


    }

    //添加历史记录 维修教程 故障案例
    public function addHistory()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);

        //判断是否浏览过
        $where["uid"] = $params["uid"];
        $where["aid"] = $params["aid"];
        $where["type"] = 2;
        $model = model("collect");
        $has = $model->where($where)->find();


        if ($has) {
            if ($model->where($where)->update(["updatetime" => time()])) {
                $result["data"] = 1;
                return json($result);
            }
        } else {
            $params["createtime"] = time();
            if ($model->insertGetId($params)) {
                $result["data"] = 2;
                return json($result);
            }
        }

        $result["status"] = 2020;
        $result["msg"] = "操作失败";
        return json($result);


    }

    //清除 检索历史 关键词 history
    public function cleanHistory()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);
        $where["uid"] = $params["uid"];
        $model = model("history");
        if ($model->where($where)->delete()) {
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "操作失败";
            return json($result);
        }
    }

    //清除历史记录 收藏表 collect
    public function cleanHistoryold()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);
        $where["uid"] = $params["uid"];
        $where["type"] = 2;
        $model = model("collect");
        if ($model->where($where)->delete()) {
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "操作失败";
            return json($result);
        }
    }

    //获取检索历史 关键字
    public function getKeyHistory()
    {
        $params = $this->request->post();
        $fillter = $params["fillter"];

        $where["uid"] = $fillter["uid"];
        $model = model("history");
        $list = $model
            ->where("status='normal'")
            ->where($where)
            ->field("id,contain,keyword")
            ->limit(8)
            ->order("updatetime desc,createtime desc")
            ->select();

        $list = collection($list)->toArray();

        $result = array("status" => 200, "msg" => "ok", "data" => []);

        if ($list) {
            $result["data"] = $list;
            return json($result);
        } else {
            $result["status"] = 2020;
            $result["msg"] = "暂无数据";
            return json($result);
        }

    }

    //添加检索历史 关键字
    public function addKeyHistory()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);

        //判断是否存在
        $where["uid"] = $params["uid"];
        $where["keyword"] = $params["keyword"];
        $where["type"] = 1;
        $where["contain"] = $params["contain"];;
        $model = model("history");
        $has = $model->where($where)->find();

        if ($has) {
            if ($model->where($where)->update(["updatetime" => time()])) {
                $result["data"] = 1;
                return json($result);
            }
        } else {
            $params["createtime"] = time();
            if ($model->insertGetId($params)) {
                $result["data"] = 2;
                return json($result);
            }
        }

        $result["status"] = 2020;
        $result["msg"] = "操作失败";
        return json($result);


    }


    //添加历史记录  备件检索
    public function addHistorysearch()
    {
        $params = $this->request->post()["fillter"];
        $result = array("status" => 200, "msg" => "ok", "data" => []);

        //判断是否浏览过
        $where["uid"] = $params["uid"];
        $where["aid"] = $params["aid"];
        $where["type"] = 2;
        $where["contype"] = 2;//检索
        $model = model("collect");
        $has = $model->where($where)->find();


        if ($has) {
            if ($model->where($where)->update(["updatetime" => time()])) {
                $result["data"] = 1;
                return json($result);
            }
        } else {
            $params["createtime"] = time();
            if ($model->insertGetId($params)) {
                $result["data"] = 2;
                return json($result);
            }
        }

        $result["status"] = 2020;
        $result["msg"] = "操作失败";
        return json($result);


    }


}
